﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GraphTest
{
    public partial class UcCanvas : UserControl
    {
        protected List<MyRect> Rs { get; set; }
        
        
        public UcCanvas()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            Rs = new List<MyRect>()
            {
                new MyRect(50, 50, 30, 30)
                {
                    Brush = Brushes.Red
                },
                new MyRect(100, 100, 40, 40)
                {
                    Brush = Brushes.Blue
                },
                new MyRect(80, 80, 20, 20)
                {
                    Brush = Brushes.Green
                }
            };
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Rs.ForEach(R =>
            {
                R.Paint(e.Graphics);
            });
        }




        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            bool shouldbeInvalidated = false;

            Rs.ForEach(R =>
            {
                var rects = Rs.Where(r => r != R).ToList();
                shouldbeInvalidated |= R.OnMouseMove(new Point(e.X, e.Y), Size, rects);
            });

            if (shouldbeInvalidated)
                Invalidate();

            (Parent as Form).Text = Rs[0].Rect.Contains(Rs[1].Rect).ToString();
        }


        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            Rs.ForEach(R =>
            {
                R.OnMouseUp(new Point(e.X, e.Y));
            });
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            Rs.ForEach(R =>
            {
                R.OnMouseDown(new Point(e.X, e.Y));
            });
        }
    }
}

